package com.iwish.server.storage;



import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

// Referenced classes of package redis.clients.jedis:
//            BinaryJedis, Jedis

public class RedisPool extends Pool
{
    private static class JedisFactory extends BasePoolableObjectFactory
    {

        public Object makeObject()
            throws Exception
        {
            Jedis jedis = new Jedis(host, port, timeout);
            jedis.connect();
            if(null != password)
                jedis.auth(password);
            if(database != 0)
                jedis.select(database);
            return jedis;
        }

        public void destroyObject(Object obj)
            throws Exception
        {
            if(obj instanceof Jedis)
            {
                Jedis jedis = (Jedis)obj;
                if(jedis.isConnected())
                    try
                    {
                        try
                        {
                            jedis.quit();
                        }
                        catch(Exception e) { }
                        jedis.disconnect();
                    }
                    catch(Exception e) { }
            }
        }

        public boolean validateObject(Object obj)
        {
            if(obj instanceof Jedis)
            {
                Jedis jedis = (Jedis)obj;
                try
                {
                    return jedis.isConnected() && jedis.ping().equals("PONG");
                }
                catch(Exception e)
                {
                    return false;
                }
            } else
            {
                return false;
            }
        }

        private final String host;
        private final int port;
        private final int timeout;
        private final String password;
        private final int database;

        public JedisFactory(String host, int port, int timeout, String password, int database)
        {
            this.host = host;
            this.port = port;
            this.timeout = timeout;
            this.password = password;
            this.database = database;
        }
    }


    public RedisPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host)
    {
        this(poolConfig, host, 6379, 2000, null, 0);
    }

    public RedisPool(String host, int port)
    {
        this(new org.apache.commons.pool.impl.GenericObjectPool.Config(), host, port, 2000, null, 0);
    }
    private  String alias;
    public String getAlisa(){
    	return this.alias;
    }
    public void setAlisa(String a){
    	this.alias=a;
    }
    public RedisPool(String host)
    {
        this(host, 6379);
    }

    public RedisPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password)
    {
        this(poolConfig, host, port, timeout, password, 0);
    }

    public RedisPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port)
    {
        this(poolConfig, host, port, 2000, null, 0);
    }

    public RedisPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port, int timeout)
    {
        this(poolConfig, host, port, timeout, null, 0);
    }

    public RedisPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password, int database)
    {
        super(poolConfig, new JedisFactory(host, port, timeout, password, database));
    }

    public void returnBrokenResource(BinaryJedis resource)
    {
        returnBrokenResourceObject(resource);
    }

    public void returnResource(BinaryJedis resource)
    {
        returnResourceObject(resource);
    }
}


/*
	DECOMPILATION REPORT

	Decompiled from: D:\sysfile\javalib\jedis-2.1.0.jar
	Total time: 709 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/